package com._54programer.demo1;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;


/*
客户端输出:
服务器：/127.0.0.1:10000  hello, 客户端!!!
 */
public class NettyClient {

    public static void main(String[] args) throws Exception{
        //客户端一个事件循环组就可以了
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            //启动类
            Bootstrap bootstrap = new Bootstrap();
            //设置启动参数
            bootstrap.group(group)//设置线程组
                    .channel(NioSocketChannel.class)//设置客户端的通道实现类
                    .handler(new ChannelInitializer<SocketChannel>() {
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline().addLast(new NettyClientHandler());//加入自己的处理器
                        }
                    });
            //启动客户端连接服务器端
            ChannelFuture cf = bootstrap.connect("127.0.0.1", 10000).sync();
            //对关闭通道进行监听
            cf.channel().closeFuture().sync();
        }finally {
            group.shutdownGracefully();
        }
    }
}